package cn.iocoder.yudao.module.wine.service.businesscredit;

import cn.iocoder.yudao.module.wine.controller.app.businesscredit.vo.AppBusinessCreditRespVO;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;

import java.util.*;
import cn.iocoder.yudao.module.wine.controller.admin.businesscredit.vo.*;
import cn.iocoder.yudao.module.wine.dal.dataobject.businesscredit.BusinessCreditDO;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;

import cn.iocoder.yudao.module.wine.dal.mysql.businesscredit.BusinessCreditMapper;

import static cn.iocoder.yudao.framework.common.exception.util.ServiceExceptionUtil.exception;
import static cn.iocoder.yudao.module.wine.enums.ErrorCodeConstants.*;

/**
 * 会员等级管理 Service 实现类
 *
 * @author 管理员
 */
@Service
@Validated
public class BusinessCreditServiceImpl implements BusinessCreditService {

    @Resource
    private BusinessCreditMapper businessCreditMapper;

    @Override
    public Long createBusinessCredit(BusinessCreditSaveReqVO createReqVO) {
        // 插入
        BusinessCreditDO businessCredit = BeanUtils.toBean(createReqVO, BusinessCreditDO.class);
        businessCreditMapper.insert(businessCredit);
        // 返回
        return businessCredit.getId();
    }

    @Override
    public void updateBusinessCredit(BusinessCreditSaveReqVO updateReqVO) {
        // 校验存在
        validateBusinessCreditExists(updateReqVO.getId());
        // 更新
        BusinessCreditDO updateObj = BeanUtils.toBean(updateReqVO, BusinessCreditDO.class);
        businessCreditMapper.updateById(updateObj);
    }

    @Override
    public void deleteBusinessCredit(Long id) {
        // 校验存在
        validateBusinessCreditExists(id);
        // 删除
        businessCreditMapper.deleteById(id);
    }

    private void validateBusinessCreditExists(Long id) {
        if (businessCreditMapper.selectById(id) == null) {
            throw exception(INFO_NOT_EXISTS);
        }
    }

    @Override
    public BusinessCreditDO getBusinessCredit(Long id) {
        return businessCreditMapper.selectById(id);
    }

    @Override
    public BusinessCreditDO getBusinessCreditByLevel(Integer levelValue){
        BusinessCreditDO businessCreditDO = businessCreditMapper.selectOne(new LambdaQueryWrapper<BusinessCreditDO>().eq(BusinessCreditDO::getLevel, levelValue));
        if (businessCreditDO == null) {
            throw exception(USER_NOT_EXISTS);
        }

        return businessCreditDO;
    }

    @Override
    public PageResult<BusinessCreditDO> getBusinessCreditPage(BusinessCreditPageReqVO pageReqVO) {
        return businessCreditMapper.selectPage(pageReqVO);
    }

    @Override
    public List<BusinessCreditDO> getBusinessCreditList() {
        return businessCreditMapper.selectList();
    }

    @Override
    public List<AppBusinessCreditRespVO> appGetBusinessCreditList() {
        return businessCreditMapper.appGetBusinessCreditList();
    }

}
